package cn.com.yang.datastruct.link;

/**
 * 有序链表
 * @since 2023.08.04
 */
public class OrderLinkedList {
    private Node head;
    private int size;
    public OrderLinkedList(){
        head =null;
        size=0;
    }

    private  class Node{
        private int data;
        private Node next;//指针指向下个节点
        public Node(int data){
            this.data = data;
        }

        @Override
        public String toString() {
            return "Node{" +
                    "data=" + data +
                    ", next=" + next +
                    '}';
        }
    }

    public void insert(int data){
        Node newNode = new Node(data);
        Node previous = null;
        Node current = head;
        while (current!=null && data > current.data){
            previous = current;
            current = current.next;
        }
        if (previous==null){
            head = newNode;
            head.next = current;
            size++;
        }else {
            newNode.next = current;
            previous.next = newNode;
            size++;
        }
    }

    //链表头部删除
    public  Object delHead(){
        Object obj = head.data;
        head=head.next;
        size --;
        return obj;
    }

    //遍历所有节点信息
    public void dispaly(){
        if (size>0){
            Node current = head;
            int tempSzie =size;
            if (tempSzie==1){
                System.out.println("["+head+"]");
                return;
            }
            while (tempSzie>0){
                if (current==head){
                    System.out.println("["+current.data+"->");
                } else if (current.next==null) {
                    System.out.println(current.data+"]");
                }else{
                    System.out.println(current.data+"->");
                }
                tempSzie--;
                current=current.next;
            }
            System.out.println();
        }

    }
}
